home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Attributes / Justification.cp < prev    next >
Encoding:
Text File  |  1997-06-28  |  2.4 KB  |  101 lines  |  [TEXT/CWIE]

  1. // Justification.cp
  2.  
  3. #ifndef Justification_h
  4. #include "Justification.h"
  5. #endif
  6. #ifndef Assert_h
  7. #include "Assert.h"
  8. #endif
  9. #ifndef Rectangle_h
  10. #include "Rectangle.h"
  11. #endif
  12.  
  13. Justification::Justification( uint32 theDenominator,
  14.                                         uint32 leading,
  15.                                         uint32 trailing )
  16.   : denominator( theDenominator ),
  17.      leadingPart( leading ),
  18.      stretchingPart( theDenominator - leading - trailing )
  19.   {
  20.     Assert( theDenominator > 0 );
  21.     Assert( leading <= theDenominator );
  22.     Assert( trailing <= theDenominator );
  23.     Assert( leading + trailing <= theDenominator );
  24.   }
  25.  
  26. uint32 Justification::LeadingPortion( uint32 total, uint32 used ) const
  27.   {
  28.     uint32 slop = ( total > used ) ? total - used : 0;
  29.  
  30.     Assert( CanMultiply( slop, leadingPart ) );
  31.     
  32.     return slop * leadingPart / denominator;
  33.   }
  34.  
  35. uint32 Justification::TrailingPortion( uint32 total, uint32 used ) const
  36.   {
  37.     // This section gets the rounding error
  38.     
  39.     uint32 slop = ( total > used ) ? total - used : 0;
  40.     Assert( CanMultiply( slop, leadingPart ) );
  41.     Assert( CanMultiply( slop, stretchingPart ) );
  42.     
  43.     return slop - (slop * leadingPart / denominator)
  44.                     - (slop * stretchingPart / denominator);
  45.   }
  46.  
  47. uint32 Justification::StretchPortion( uint32 total, uint32 used ) const
  48.   {
  49.     uint32 slop = ( total > used ) ? total - used : 0;
  50.  
  51.     Assert( CanMultiply( slop, stretchingPart ) );
  52.     
  53.     return slop * stretchingPart / denominator;
  54.   }
  55.  
  56. void Justification::InsetWidth( Rectangle& target, uint32 newWidth ) const
  57.   {
  58.     uint32 oldWidth = target.Width();
  59.     if ( newWidth >= oldWidth )
  60.         return;
  61.     
  62.     target.left += LeadingPortion( oldWidth, newWidth );
  63.     target.right -= TrailingPortion( oldWidth, newWidth );
  64.     Assert( stretchingPart > 0 || target.Width() == newWidth );
  65.   }
  66.  
  67. void Justification::InsetHeight( Rectangle& target, uint32 newHeight ) const
  68.   {
  69.     uint32 oldHeight = target.Height();
  70.     if ( newHeight >= oldHeight )
  71.         return;
  72.     
  73.     target.top += LeadingPortion( oldHeight, newHeight );
  74.     target.bottom -= TrailingPortion( oldHeight, newHeight );
  75.     Assert( stretchingPart > 0 || target.Height() == newHeight );
  76.   }
  77.  
  78. const Justification& Justification::Leading()
  79.   {
  80.     static Justification leading( 1, 0, 1 );
  81.     return leading;
  82.   }
  83.  
  84. const Justification& Justification::Trailing()
  85.   {
  86.     static Justification trailing( 1, 1, 0 );
  87.     return trailing;
  88.   }
  89.  
  90. const Justification& Justification::Center()
  91.   {
  92.     static Justification center( 2, 1, 1 );
  93.     return center;
  94.   }
  95.  
  96. const Justification& Justification::Full()
  97.   {
  98.     static Justification full( 1, 0, 0 );
  99.     return full;
  100.   }
  101.